/*
package nr.nodehost;

public class Parser {
	public static final int DETECTING_HEAD_BYTES=0;
	public static final int DETECTING_LENGTH=1;
	public static final int RECEIVING_HEAD=2;
	public static final int RECEIVING_DATA=3;
	public static final int FRAME_HEAD_BYTE0=0xEE;
	public static final int FRAME_HEAD_BYTE1=0xEF;
	public static final int MAX_FRAME_LENGTH=256*1024*1024;

	private Dispatcher dispatcher;
	private String schemaRunDir;
	private byte[] frameData=null;//new byte[MAX_FRAME_LENGTH];
	private int frameLength=0;//actual frame length
	private int pendingLength=0;
	private int status=DETECTING_HEAD_BYTES;
	DataFrame frame=null;

	public Parser(int maxFrameLength,Dispatcher dispatcher,String schemaRunDir) {
		this.dispatcher=dispatcher;
		this.schemaRunDir=schemaRunDir;
		frameData=new byte[maxFrameLength*1024*1024];
	}
	public int parse(byte[] data,int length) {
		if(pendingLength+length>frameData.length) {
			System.out.println("buffer full\n");
			return -1;
		}

		System.arraycopy(data, 0, frameData, pendingLength, length);
		pendingLength+=length;

		if(status==DETECTING_HEAD_BYTES) {
			int skipLength=0;
			boolean headFound=false;
			//search 2 bytes frame head
			while((pendingLength-skipLength)>=2&&!headFound) {
				headFound=((0xFF&frameData[skipLength])==FRAME_HEAD_BYTE0||(0xFF&frameData[skipLength+1])==FRAME_HEAD_BYTE1);
				skipLength+=2;
			}
			if(headFound)
				status=DETECTING_LENGTH;
			if(skipLength>0) {
				System.arraycopy(frameData, skipLength, frameData, 0, pendingLength-skipLength);
				pendingLength-=skipLength;
			}
		}
		if(status==DETECTING_LENGTH) {
			if(pendingLength<4)
				return 0;
			frameLength=Utils.bytes2long(frameData,0)-4;//exclude length field
			System.arraycopy(frameData, 4, frameData, 0, pendingLength-4);
			pendingLength-=4;
			status=RECEIVING_HEAD;
		}
		if(status==RECEIVING_HEAD) {
			if(pendingLength<DataFrame.HEADER_LENGTH)
				return 0;
			frame= DataFrame.fromDataBytes(frameData,frameLength,schemaRunDir);
			if(frame==null) {
				System.out.println("create data frame fail.");
				return -1;
			}
			if(pendingLength>DataFrame.HEADER_LENGTH) {
				System.arraycopy(frameData, DataFrame.HEADER_LENGTH,frameData, 0,  pendingLength-DataFrame.HEADER_LENGTH);
				pendingLength-=DataFrame.HEADER_LENGTH;
				status=RECEIVING_DATA;
				frameLength-=DataFrame.HEADER_LENGTH;
			}
		}
		if(status==RECEIVING_DATA) {
			if(pendingLength<=0)
				return 0;

			int dataLength=pendingLength<frameLength?pendingLength:(int)frameLength;
			frame.appendData(frameData, DataFrame.HEADER_LENGTH,dataLength);
			if(pendingLength>frameLength) {
				System.arraycopy(frameData, (int)frameLength,frameData, 0,  pendingLength-(int)frameLength);
			}
			pendingLength-=dataLength;
			frameLength-=dataLength;
			if(frameLength==0) {
				SchemaExecutor schemaExecutor=dispatcher.getSchemaExecutor(frame.getSchemaId());
				if(schemaExecutor!=null) {
					//wait schema execute if frame no. greater than executed number
					while(schemaExecutor.getExecutedNumber()+2<frame.getFrameNo()) {
						try{
							System.out.println("data frame no. is greater than schema's executed number,waiting...");
							Thread.sleep(200);
						}
						catch(Exception e) {
							e.printStackTrace();
						}
					}
				}
				else {
					System.out.println("schema "+frame.getSchemaId()+" isn't running ");
					frame.deleteDataFile();
					frame=null;
					System.gc();
				}
				dispatcher.dataArrive(frame);
				status=DETECTING_HEAD_BYTES;
			}
		}
		return 1;
	}
}
*/
